import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Created by IntelliJ IDEA.
 * User: Awake
 * Date: 01.02.12
 * Time: 20:55
 * To change this template use File | Settings | File Templates.
 */
public class Calculator {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                CalcFrame frame = new CalcFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

class CalcFrame extends JFrame {

    public CalcFrame() {
        lastCommand = "=";
        start = true;
        setSize(def_Width, def_Height);
        setTitle("Calculator");
        setResizable(false);

        ActionListener insert = new InsertAction();
        ActionListener command = new CommandAction();

        upperPanel = new JPanel();
        upperPanel.setLayout(new GridLayout(1, 1));
        add(upperPanel, BorderLayout.NORTH);
        numberField = new JTextField();
        numberField.setEditable(false);
        upperPanel.add(numberField);

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(4, 4));

        addButton("7", insert);
        addButton("8", insert);
        addButton("9", insert);
        addButton("/", command);

        addButton("4", insert);
        addButton("5", insert);
        addButton("6", insert);
        addButton("*", command);

        addButton("1", insert);
        addButton("2", insert);
        addButton("3", insert);
        addButton("-", command);

        addButton("0", insert);
        addButton("C", command);
        addButton("=", command);
        addButton("+", command);

        add(buttonPanel, BorderLayout.CENTER);
    }

    private class InsertAction implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            String input = event.getActionCommand();
            if (start) {
                numberField.setText("");
                start = false;
            }
            numberField.setText(numberField.getText() + input);
        }
    }

    private class CommandAction implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            String command = event.getActionCommand();
            if (command.equals("C")) {
                numberField.setText("");
                firstArg = 0;
                secondArg = 0;
                start = true;
            } else if (!start) {
                if (command.equals("=") && !(lastCommand.equals("="))) {
                    try {
                        secondArg = Integer.parseInt(numberField.getText());
                        numberField.setText("" + Calc.operation(firstArg, secondArg, lastCommand));
                        firstArg = 0;
                        secondArg = 0;
                        lastCommand = "=";
                        start = true;
                    } catch (NumberFormatException e) {
                        numberField.setText("Argument is out of range");
                      //  firstArg = 0;
                        start = true;
                    }

                } else {
                    lastCommand = command;
                    try {
                        firstArg = Integer.parseInt(numberField.getText());
                        numberField.setText(command);
                        start = true;
                    } catch (NumberFormatException e) {
                        numberField.setText("Argument is out of range");
                        start = true;
                    }

                }
            }
        }
    }

    private void addButton(String name, ActionListener listener) {
        JButton button = new JButton(name);
        button.addActionListener(listener);
        buttonPanel.add(button);
    }

    private String lastCommand;
    private int firstArg;
    private int secondArg;
    private boolean start;
    private JPanel upperPanel;
    private JPanel buttonPanel;
    private JTextField numberField;
    public static final int def_Width = 300;
    public static final int def_Height = 300;
}



